// commons/diy/activity/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    activity_id: {
      type: Number,
      value: 0
    },
    back_end: {
      type: String,
      value: '#ffffff'
    },
    back_start: {
      type: String,
      value: '#ffffff'
    },
    all_color: {
      type: String,
      value: '#f90000'
    },
    is_diy: {
      type: Number,
      value: 0
    },
    setting: {
      type: Object,
      value: null
    }
  },
  ready: function () {
    this.getActivity();
    this.setData({
      __wxapp_img: wx.getStorageSync('wxapp_img')
    })
  },

  /**
   * 组件的初始数据
   */
  data: {
    activity: {}
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getActivity: function () {
      var that = this;
      var shop_id = wx.getStorageSync('address_info').shop_id;
      getApp().request({
        url: getApp().api.index.get_diy_info,
        data: {
          shop_id: shop_id,
          name: 'activity',
          activity_id: that.data.activity_id,
          limit: that.properties.setting && that.properties.setting.goods_num ? that.properties.setting.goods_num : 5
        },
        success: function(t) {
          that.setData({
            activity: t.data.list
          }, function() {
            that.setTime()
          })
        },
        complete: function() {
          
        }
      })
    },
    showpicker: function (t) {
      this.triggerEvent('showpicker',t.currentTarget.dataset)
    },// 时间倒退
    setTime() {
      var that =  this;

      setInterval(function () {
        var date = new Date();
        var now = date.getTime() / 1000;
        now = Math.round(now);
        var activity = that.data.activity;
        if (activity) {
          var let_time = activity.end_time - now;
          var start_time = activity.start_time - now;
          if (start_time > 0) {
            var d = Math.floor(start_time / 60 / 60 / 24);
            var h = Math.floor(start_time / 60 / 60 % 24);
            var m = Math.floor(start_time / 60 % 60);
            var s = Math.floor(start_time % 60);
            activity.s = s < 10 ? "0" + s : s;
            activity.m = m < 10 ? "0" + m : m;
            activity.h = h < 10 ? "0" + h : h;
            activity.d = d < 10 ? "0" + d : d;
            activity.time_type = 2; //还没开始
          } else if (let_time >= 0) {
            var d = Math.floor(let_time / 60 / 60 / 24);
            var h = Math.floor(let_time / 60 / 60 % 24);
            var m = Math.floor(let_time / 60 % 60);
            var s = Math.floor(let_time % 60);
            activity.s = s < 10 ? "0" + s : s;
            activity.m = m < 10 ? "0" + m : m;
            activity.h = h < 10 ? "0" + h : h;
            activity.d = d < 10 ? "0" + d : d;
            activity.time_type = 1; // 还没结束
  
          } else {
            activity.s = '00';
            activity.m = '00';
            activity.h = '00';
            activity.d = '00';
            activity.time_type = 0; // 已经结束
          }
        }
        that.setData({
          activity: activity,
        })


      }, 1000)
      
      
    },
  
  }
})
